package com.jokey.vue.controller;

import com.jokey.vue.dto.EmpPageReq;
import com.jokey.vue.model.Employee;
import com.jokey.vue.model.RespBean;
import com.jokey.vue.service.*;
import com.jokey.vue.utils.POIUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author JokeyFeng
 * @date: 2021/11/27
 * @project: vuehr
 * @package: com.jokey.vue.controller
 * @comment:
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private NationService nationService;

    @Resource
    private PositionService positionService;

    @Resource
    private JobLevelService jobLevelService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private PoliticsStatusService politicsStatusService;

    @PostMapping("")
    public RespBean addEmp(@RequestBody Employee employee) {
        boolean save = employeeService.save(employee);
        return save ? RespBean.ok("添加成功") : RespBean.error("添加失败");
    }

    @PutMapping("")
    public RespBean updateEmp(@RequestBody Employee employee) {
        boolean save = employeeService.updateById(employee);
        return save ? RespBean.ok("修改成功") : RespBean.error("修改失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteEmp(@PathVariable Integer id) {
        boolean delete = employeeService.removeById(id);
        return delete ? RespBean.ok("修改成功") : RespBean.error("修改失败");
    }

    @PostMapping("/page")
    public RespBean queryPage(@RequestBody EmpPageReq pageReq) {
        return RespBean.ok(employeeService.queryPage(pageReq));
    }

    @GetMapping("/nations")
    public RespBean getAllNations() {
        return RespBean.ok(nationService.list());
    }

    @GetMapping("/politicsStatus")
    public RespBean getAllPoliticsStatus() {
        return RespBean.ok(politicsStatusService.list());
    }

    @GetMapping("/jobLevels")
    public RespBean getAllJobLevels() {
        return RespBean.ok(jobLevelService.list());
    }

    @GetMapping("/positions")
    public RespBean getAllPositions() {
        return RespBean.ok(positionService.list());
    }

    @GetMapping("/maxWorkId")
    public RespBean maxWorkId() {
        return RespBean.build()
                .setObj(String.format("%08d", employeeService.maxWorkId() + 1));
    }

    @GetMapping("/departments")
    public RespBean getAllDepartments() {
        return RespBean.ok(departmentService.list());
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData() {
        EmpPageReq pageReq = new EmpPageReq();
        pageReq.setSize(1000);
        List<Employee> list = employeeService.queryPage(pageReq).getList();
        return POIUtils.employee2Excel(list);
    }

    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
        List<Employee> list = POIUtils.excel2Employee(file, nationService.list(), politicsStatusService.list(), departmentService.list(), positionService.list(), jobLevelService.list());
        if (employeeService.saveBatch(list)) {
            return RespBean.ok("上传成功");
        }
        return RespBean.error("上传失败");
    }

}
